package controllers;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Vector;

import windows.GameConsole;

public class DataInsertion {
		
	//Kan uit iedere classe zo aangeroepen worden: DataInsertion.LogInsert()
	public static void LogInsert(String userInsert, String actionInsert){
		
		Connection con = null;
	    try {
	      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	      con = DriverManager.getConnection("jdbc:odbc:HY_ACCESS");
	      Statement sta = con.createStatement(); 
	
	      // insert 1 row	      
	      int c = sta.executeUpdate("INSERT INTO Data (user, action, `time`) VALUES ('" + userInsert + "', '" + actionInsert + "', NOW())");
	      
	      int count = 1;
	
	      sta.close();
	      con.close();        
	    } catch (Exception e) {
	      //System.err.println("Exception: "+e.getMessage());
		}
    }
	
public static void ScenarioInsert(String scenName, String wpX, String wpY){
		
		Connection con = null;
	    try {
	      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	      con = DriverManager.getConnection("jdbc:odbc:HY_ACCESS");
	      Statement sta = con.createStatement(); 
	
	      // insert 1 row	      
	      int c = sta.executeUpdate("INSERT INTO Scenario (Scenarioname, Waypoint_X ,Waypoint_Y, `time`) VALUES ('" + scenName + "', '" + wpX + "','" + wpY + "', NOW())");
	      
	      int count = 1;
	
	      sta.close();
	      con.close();        
	    } catch (Exception e) {
	      //System.err.println("Exception: "+e.getMessage());
		}
    }

public static void autoLogInsert(String autoObjectInsert, String autoValInsert){
	
	Connection con = null;
	String playerName = GameConsole.UserName.toString();
    try {
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      con = DriverManager.getConnection("jdbc:odbc:HY_ACCESS");
      Statement sta = con.createStatement(); 

      // insert 1 row	      
      int c = sta.executeUpdate("INSERT INTO autoLog (object, val, `time`, playername) VALUES ('" + autoObjectInsert + "', '" + autoValInsert + "', NOW(), '" + playerName + "')");
      
      int count = 1;

      sta.close();
      con.close();        
    } catch (Exception e) {
      //System.err.println("Exception: "+e.getMessage());
	}
}
	    
	public static void LogSelect(String userSelect, String actionSelect){
			
			Connection conn = null;
			Statement stmt = null;
			
			   try{
			      //STEP 2: Register JDBC driver
				   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

			      //STEP 3: Open a connection
			      System.out.println("Connecting to a selected database...");
			      conn = DriverManager.getConnection("jdbc:odbc:HY_ACCESS");
			      System.out.println("Connected database successfully...");
			      
			      //STEP 4: Execute a query
			      System.out.println("Creating statement...");
			      stmt = conn.createStatement();

			      String sql = "SELECT id, user, action, time FROM data";
			      ResultSet rs = stmt.executeQuery(sql);
			      //STEP 5: Extract data from result set
			      while(rs.next()){
			         //Retrieve by column name
			         int id  = rs.getInt("id");
			         String user = rs.getString("user");
			         String action = rs.getString("action");
			         String time = rs.getString("time");

			         //Display values
			         System.out.print("ID: " + id);
			         System.out.print(", User: " + user);
			         System.out.print(", Action: " + action);
			         System.out.println(", Time: " + time);
			      }
			      rs.close();
				   }
			   catch(Exception e){
				      //Handle errors for JDBC
			     // e.printStackTrace();
			   }finally{
			      //finally block used to close resources
			      try{
			         if(stmt!=null)
			            conn.close();
			      }catch(Exception e){
			      }// do nothing
			      try{
			         if(conn!=null)
			            conn.close();
			      }catch(Exception e){
			        // e.printStackTrace();
			      }//end finally try
			   }//end try
	}
	
	public static String ScenarioSelect(String nameSelect){
		
		Connection conn = null;
		Statement stmt = null;
		
		   String sqlOutput = "";
		try{
		      //STEP 2: Register JDBC driver
			   Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

		      //STEP 3: Open a connection
		      System.out.println("Connecting to a selected database...");
		      conn = DriverManager.getConnection("jdbc:odbc:HY_ACCESS");
		      System.out.println("Connected database successfully...");
		      
		      //STEP 4: Execute a query
		      System.out.println("Creating statement...");
		      stmt = conn.createStatement();
		      
		      String sql = "SELECT id, Scenarioname, Waypoint_X, Waypoint_Y,  time FROM Scenario where Scenarioname = '" + nameSelect + "';";

		      ResultSet rs = stmt.executeQuery(sql);
		      //STEP 5: Extract data from result set
		      while(rs.next()){
		         //Retrieve by column name
		         int id  = rs.getInt("id");
		         String scenName = rs.getString("Scenarioname");
		         String X_coordinat = rs.getString("Waypoint_X");
		         String Y_coordinat = rs.getString("Waypoint_Y");
		         String time = rs.getString("time");
		         sqlOutput= sqlOutput +  X_coordinat + "Z" + Y_coordinat + "Z";
		         //Display values
		         System.out.print("ID: " + id);
		         System.out.print(", Scenarioname: " + scenName);
		         System.out.print(", XSelect: " + X_coordinat);
		         System.out.print(", YSelect: " + Y_coordinat);
		         System.out.println(", Time: " + time);
		         
		      }
		      
		      rs.close();
			   }
		   
		   catch(Exception e){
			      //Handle errors for JDBC
		      //e.printStackTrace();
		   }finally{
		      //finally block used to close resources
		      try{
		         if(stmt!=null)
		            conn.close();
		      }catch(Exception e){
		      }// do nothing
		      try{
		         if(conn!=null)
		            conn.close();
		      }catch(Exception e){
		        // e.printStackTrace();
		      }//end finally try
		      
		   }//end try
		   return sqlOutput;
}
}
